package com.ruoyi.dataSet.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.dataSet.DTO.DocumentSegmentCreateRequest;
import com.ruoyi.dataSet.DTO.DocumentSegmentDeleteRequest;
import com.ruoyi.dataSet.DTO.DocumentSegmentUpdateRequest;
import com.ruoyi.dataSet.service.DocumentSegmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
@Anonymous
@RestController
@RequestMapping("/segment")
public class DocumentSegmentController {

    @Autowired
    private DocumentSegmentService documentSegmentService;

    // 创建文档分段
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('deepSeek:segment:add')")
    public String createSegment(@RequestBody DocumentSegmentCreateRequest request) {
        try {
            String datasetId = request.getDatasetId();
            String documentId = request.getDocumentId();
            return documentSegmentService.createSegment(datasetId, documentId, request);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 获取文档分段
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('deepSeek:segment:list')")
    public String getSegments(@RequestBody DocumentSegmentCreateRequest request, @RequestParam String page, @RequestParam String limit) {
        try {
            String datasetId = request.getDatasetId();
            String documentId = request.getDocumentId();
            return documentSegmentService.getSegments(datasetId, documentId, page, limit);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 删除文档分段
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('deepSeek:segment:delete')")
    public String deleteSegment(@RequestBody DocumentSegmentDeleteRequest documentSegmentDeleteRequest) {
        try {
            String datasetId = documentSegmentDeleteRequest.getDatasetId();
            String documentId = documentSegmentDeleteRequest.getDocumentId();
            String segmentId = documentSegmentDeleteRequest.getSegmentId();
            return documentSegmentService.deleteSegment(datasetId, documentId, segmentId);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    // 更新文档分段
    @PutMapping("/update/{datasetId}/{documentId}/{segmentId}")
    @PreAuthorize("@ss.hasPermi('deepSeek:segment:update')")
    public String updateSegment(@PathVariable String datasetId, @PathVariable String documentId, @PathVariable String segmentId, @RequestBody DocumentSegmentUpdateRequest request) {
        try {
            return documentSegmentService.updateSegment(datasetId, documentId, segmentId, request);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
}
